/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:34 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class BaikiemtratuluanBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Baikiemtratuluan Này";
		private static string EX_FK_ITHONGTINBAIKIEMTRAID_INVALID="FK_iThongtinbaikiemtraID không hợp lệ";
		private static string EX_FK_ICAUHOIID_INVALID="FK_iCauhoiID không hợp lệ";
		
		private static string EX_IDIEM_INVALID="iDiem không hợp lệ";
		private static string EX_ID_INVALID="FK_iThongtinbaikiemtraID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Baikiemtratuluan Theo ID
        /// </summary>
        /// <param name="FK_iThongtinbaikiemtraID">Int64:Baikiemtratuluan ID</param>
        /// <returns>BaikiemtratuluanEntity</returns>        
        public static BaikiemtratuluanEntity GetOne(Int64 FK_iThongtinbaikiemtraID, Int64 FK_iCauhoiID)
        {
            
			if(FK_iThongtinbaikiemtraID<=0||FK_iCauhoiID<=0)
				throw new Exception(EX_ID_INVALID);
            return BaikiemtratuluanDAL.GetOne(FK_iThongtinbaikiemtraID, FK_iCauhoiID);
        }
        /// <summary>
        /// Lấy về List các Baikiemtratuluan
        /// </summary>
        /// <returns>List BaikiemtratuluanEntity:List Baikiemtratuluan Cần lấy</returns>
        public static List<BaikiemtratuluanEntity> GetAll()
        {
            return BaikiemtratuluanDAL.GetAll();
        }
        
        /// <summary>
        /// Kiểm tra và thêm mới Baikiemtratuluan
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Baikiemtratuluan Mới Thêm Vào</returns>
        public static Int32 Add(BaikiemtratuluanEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return BaikiemtratuluanDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Baikiemtratuluan
        /// </summary>
        /// <param name="entity">BaikiemtratuluanEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(BaikiemtratuluanEntity entity)
        {
            checkExist(entity.FK_iThongtinbaikiemtraID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return BaikiemtratuluanDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Baikiemtratuluan
        /// </summary>
        /// <param name="FK_iThongtinbaikiemtraID">Int64 : FK_iThongtinbaikiemtraID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 FK_iThongtinbaikiemtraID)
        {
            checkExist(FK_iThongtinbaikiemtraID);
            return BaikiemtratuluanDAL.Remove(FK_iThongtinbaikiemtraID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 FK_iThongtinbaikiemtraID)
        {
            //BaikiemtratuluanEntity oBaikiemtratuluan=BaikiemtratuluanDAL.GetOne(FK_iThongtinbaikiemtraID);
            //if(oBaikiemtratuluan==null)
            //    throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">BaikiemtratuluanEntity: entity</param>
        private static void checkLogic(BaikiemtratuluanEntity entity)
        {   
			if (entity.FK_iThongtinbaikiemtraID < 0)
				throw new Exception(EX_FK_ITHONGTINBAIKIEMTRAID_INVALID);
			if (entity.FK_iCauhoiID < 0)
				throw new Exception(EX_FK_ICAUHOIID_INVALID);
			
			if (entity.fDiem < 0)
				throw new Exception(EX_IDIEM_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">BaikiemtratuluanEntity: BaikiemtratuluanEntity</param>
        private static void checkDuplicate(BaikiemtratuluanEntity entity,bool checkPK)
        {
            /* 
            Example
            List<BaikiemtratuluanEntity> list = BaikiemtratuluanDAL.GetAll();
            if (list.Exists(
                delegate(BaikiemtratuluanEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.FK_iThongtinbaikiemtraID != entity.FK_iThongtinbaikiemtraID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">BaikiemtratuluanEntity:entity</param>
        private static void checkFK(BaikiemtratuluanEntity entity)
        {            
        }
        #endregion
    }
}
